package com.wardrobe.soa.api.store;

import com.wardrobe.dao.store.StoreDao;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.store.StoreBean;
import com.wardrobe.domain.store.StoreShowBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/11/20.
 */
@Service
public class StoreService {
    @Autowired
    private StoreDao storeDao;

    /**
     * 预约收藏列表
     * @return
     */
    public List<StoreShowBean> getStoreBookingList(long userId){
       return storeDao.selectStoreBookingList(userId);
    }

    /**
     * 商品收藏列表
     * @param
     * @return
     */
    public List<StoreShowBean> getStoreGoodsList(long userId){
        return storeDao.selectStoreGoodsList(userId);
    }
    public int saveStore(StoreBean storeBean){
        if (storeBean.getType()==1){
            StoreShowBean storeShowBean = storeDao.selectStoreGoods(storeBean.getUserId(), storeBean.getStoreId());
            StoreShowBean cancelStoreShow = storeDao.selectCancelStoreGoods(storeBean.getUserId(), storeBean.getStoreId());
            if(cancelStoreShow!=null){
                BackEndFromBean fromBean = new BackEndFromBean();
                fromBean.setKey("status");
                fromBean.setValue("1");
                fromBean.setUpdateTime(new Date());
                fromBean.setId(cancelStoreShow.getId());
                return storeDao.updateStore(fromBean);
            }
            if(storeShowBean!=null){
                return -1;
            }else {
                return storeDao.insertStore(storeBean);
            }
        }else{
            StoreShowBean storeShowBean = storeDao.selectStoreBooking(storeBean.getUserId(), storeBean.getStoreId());
            StoreShowBean cancelStoreShow = storeDao.selectCancelStoreBooking(storeBean.getUserId(), storeBean.getStoreId());
            if(cancelStoreShow!=null){
                BackEndFromBean fromBean = new BackEndFromBean();
                fromBean.setKey("status");
                fromBean.setValue("1");
                fromBean.setUpdateTime(new Date());
                fromBean.setId(cancelStoreShow.getId());
                return storeDao.updateStore(fromBean);
            }
            if(storeShowBean!=null){
                return -1;
            }else {
                return storeDao.insertStore(storeBean);
            }
        }
    }

    /**
     * 取消收藏
     * @param fromBean
     * @return
     */
    public int modifyStore(BackEndFromBean fromBean){
        return storeDao.updateStore(fromBean);
    }
}
